package org.amphiprion.montrictracv4;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.amphiprion.montrictracv4.db.model.Ludotheque;
import org.amphiprion.montrictracv4.db.model.Search;
import org.amphiprion.montrictracv4.web.util.cache.ImageCache;
import org.amphiprion.montrictracv4.web.util.cache.ImageFetcher;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;

public class App {
	public static Ludotheque currentLudotheque;
	public static Search currentSearch;

	private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
	private static ImageFetcher mImageFetcher;

	public static int dpiToPixels(Context paramContext, int paramInt) {
		return (int) (0.5F + paramInt * paramContext.getResources().getDisplayMetrics().density);
	}

	public static boolean isPhone(Context paramContext) {
		return !isTablet(paramContext);
	}

	public static boolean isTablet(Context paramContext) {
		if ((0xF & paramContext.getResources().getConfiguration().screenLayout) >= 3) {
			return true;
		} else {
			return false;
		}
	}

	public static void hideKeyboard(Context context, EditText editText) {
		InputMethodManager localInputMethodManager = (InputMethodManager) context.getSystemService("input_method");
		if (localInputMethodManager != null) {
			localInputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);
		}
	}

	public static boolean isNetworkAvail(Context context) {
		NetworkInfo localNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
		if ((localNetworkInfo == null) || (!localNetworkInfo.isConnected())) {
			return false;
		} else {
			return true;
		}
	}

	public static String getValue(String paramString) {

		if ((paramString != null) && (!paramString.equals("null")) && (!TextUtils.isEmpty(paramString))) {
			return paramString.trim();
		}
		return "";
	}

	public static int getInt(String value, int defaultValue) {
		try {
			return Integer.parseInt(value.trim());
		} catch (Throwable e) {
			return defaultValue;
		}
	}

	public static String formatDate(Date date) {
		return dateFormat.format(date);
	}

	// public static void setUrlDrawable(final ImageView imageView,
	// final String url) {
	// new Thread() {
	// @Override
	// public void run() {
	// Bitmap x;
	// try {
	// HttpURLConnection connection = (HttpURLConnection) new URL(
	// url).openConnection();
	// connection.connect();
	// InputStream input = connection.getInputStream();
	//
	// x = BitmapFactory.decodeStream(input);
	// imageView.setImageBitmap(x);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// }.start();
	//
	// }

	public static void setUrlDrawable(ImageView paramImageView, String paramString, FragmentManager paramFragmentManager) {
		if (mImageFetcher == null) {
			ImageCache.ImageCacheParams localImageCacheParams = new ImageCache.ImageCacheParams(paramImageView.getContext(), "tempimgs");
			localImageCacheParams.setMemCacheSizePercent(0.2F);
			mImageFetcher = new ImageFetcher(paramImageView.getContext(), paramImageView.getWidth(), paramImageView.getHeight());
			mImageFetcher.addImageCache(paramFragmentManager, localImageCacheParams);
		}
		mImageFetcher.loadImage(paramString, paramImageView);
	}

}
